# This Python file uses the following encoding: utf-8
from django.db import models
from django.contrib.auth.models import User
from curriculums.models import Cicle, ModulProfessional, UnitatFormativa, Contingut 
from django.template.defaultfilters import slugify

class Post(models.Model):
    TIPUS_CHOICES = (
            ('T', u'Teoria'),
            ('P', u'Pràctica'),
            ('P', u'Programació Curricular'),
                     )
    
    titol = models.CharField(u"Títol", max_length=250, help_text = u'''Títol''', blank = False, unique = True )
    cos = models.TextField(u"Cos",  help_text = u'''Cos del post''', blank = False )
    tipus = models.CharField(u"Tipus", max_length=1, choices = TIPUS_CHOICES, help_text = u'''Tipus''', blank = False )
    moment = models.DateTimeField(u'Moment Publicació',  auto_now_add = True,  db_index = True, help_text = u'''Data de publicació d'aquest POST''' )
    usuari_post = models.ForeignKey(User, related_name = "posts_publicats")
    usuari_darrer_canvi = models.ForeignKey(User, related_name = "posts_modificats")
    cicles = models.ManyToManyField( Cicle )
    unitats_formatives = models.ManyToManyField( UnitatFormativa )
    continguts = models.ManyToManyField( Contingut )
    esborrat = models.BooleanField( u"Post Esborrat", default = False, blank = False  )
    nVotsPositius = models.IntegerField( u"Vots Positius", default = 0, blank = False )
    nVotsNegatius = models.IntegerField( u"Vots Negatius", default = 0, blank = False )
    slug = models.SlugField(db_index = True, unique = True)
    
    def save(self, *args, **kwargs):
        self.slug = self.slug if self.slug else self.my_slug()
        super(Post, self).save(*args, **kwargs)
        
    def my_slug(self):
        slug_txt = self.titol
        return slugify( slug_txt )

    class Meta:
        ordering=['-moment']
        db_table = u'posts'
        verbose_name = u'Post'
        verbose_name_plural = u'Posts'
        
    def __unicode__(self):
        return u"{0}".format( self.titol )
    
class HistorialPost(models.Model):
    post = models.ForeignKey( Post, db_index = True )
    usuari_canvi = models.ForeignKey(User)
    moment_canvi = models.DateTimeField(u'Moment Canvi',  auto_now_add = True,  db_index = True, help_text = u'''Data de modificació del post''' )

    titol_bkup = models.CharField(u"Títol", max_length=250, help_text = u'''Títol''', blank = False, unique = True )
    cos_bkup = models.TextField(u"Cos",  help_text = u'''Cos del post''', blank = False )
    tipus_bkup = models.CharField(u"Tipus", max_length=1, choices = Post.TIPUS_CHOICES, help_text = u'''Tipus''', blank = False )
    cicles_bkup = models.ManyToManyField( Cicle )
    moduls_professionals_bkup = models.ManyToManyField( ModulProfessional )
    unitats_formatives_bkup = models.ManyToManyField( UnitatFormativa )
    continguts_bkup = models.ManyToManyField( Contingut )
    esborrat_bkup = models.BooleanField( u"Post Esborrat", default = False, blank = False  )
    
    class Meta:
        ordering=['post','-moment_canvi']
        db_table = u'posts_historial'
        verbose_name = u'Mòdul Professional'
        verbose_name_plural = u'Mòduls Professional'
        
    def __unicode__(self):
        return u"{0}".format( self.titol_bkup )


class Comentaris(models.Model):
    post = models.ForeignKey( Post, db_index = True )
    texte = models.TextField(u"Comentari",  help_text = u'''Comentari''', blank = False )
    usuari_comenta = models.ForeignKey(User)
    moment_comentari = models.DateTimeField(u'Moment Comentari',  auto_now_add = True,  help_text = u'''Data del comentari''' )
    nVotsPositius = models.IntegerField( u"Vots Positius", default = 0, blank = False )

    class Meta:
        ordering=['post','moment_comentari']
        db_table = u'comentaris'
        verbose_name = u'Comentari'
        verbose_name_plural = u'Comentaris'
        
    def __unicode__(self):
        return u"{0}-{1}".format( self.usuari_comenta, self.texte )

        